import { defineStore } from 'pinia'
import { getRecipeDetail, getRecipes, getRecipeFilters } from '../api/recipe'

export const useRecipeStore = defineStore('recipe', {
  state: () => ({
    recipeCache: {}, // 缓存食谱详情，key是食谱ID
    recipeListCache: {}, // 缓存食谱列表，key是查询参数的JSON字符串
    filters: null, // 筛选条件
    lastFetchTimes: {}, // 记录上次获取数据的时间，key是'detail-{id}'或'list-{paramsKey}'
    cacheDuration: 5 * 60 * 1000, // 缓存有效期，默认5分钟
  }),
  
  actions: {
    // 获取食谱详情，优先从缓存获取
    async fetchRecipeDetail(id, userId) {
      const cacheKey = `detail-${id}`;
      const now = Date.now();
      
      // 如果有缓存且未过期，直接返回缓存
      if (
        this.recipeCache[id] && 
        this.lastFetchTimes[cacheKey] && 
        now - this.lastFetchTimes[cacheKey] < this.cacheDuration
      ) {
        return this.recipeCache[id];
      }
      
      // 否则请求接口
      try {
        const res = await getRecipeDetail(id, userId);
        if (res.code === 200) {
          // 更新缓存
          this.recipeCache[id] = res.data;
          this.lastFetchTimes[cacheKey] = now;
        }
        return res;
      } catch (error) {
        console.error('获取食谱详情失败', error);
        throw error;
      }
    },
    
    // 获取食谱列表，优先从缓存获取
    async fetchRecipeList(params) {
      const paramsKey = JSON.stringify(params);
      const cacheKey = `list-${paramsKey}`;
      const now = Date.now();
      
      // 如果有缓存且未过期，直接返回缓存
      if (
        this.recipeListCache[paramsKey] && 
        this.lastFetchTimes[cacheKey] && 
        now - this.lastFetchTimes[cacheKey] < this.cacheDuration
      ) {
        return this.recipeListCache[paramsKey];
      }
      
      // 否则请求接口
      try {
        const res = await getRecipes(params);
        if (res.code === 200) {
          // 更新缓存
          this.recipeListCache[paramsKey] = res.data;
          this.lastFetchTimes[cacheKey] = now;
        }
        return res.data;
      } catch (error) {
        console.error('获取食谱列表失败', error);
        throw error;
      }
    },
    
    // 获取筛选条件，优先从缓存获取
    async fetchFilters() {
      const now = Date.now();
      const cacheKey = 'filters';
      
      // 如果有缓存且未过期，直接返回缓存
      if (
        this.filters && 
        this.lastFetchTimes[cacheKey] && 
        now - this.lastFetchTimes[cacheKey] < this.cacheDuration
      ) {
        return this.filters;
      }
      
      // 否则请求接口
      try {
        const res = await getRecipeFilters();
        if (res.code === 200) {
          // 更新缓存
          this.filters = res.data;
          this.lastFetchTimes[cacheKey] = now;
        }
        return res.data;
      } catch (error) {
        console.error('获取筛选条件失败', error);
        throw error;
      }
    },
    
    // 清除指定食谱的缓存
    clearRecipeCache(id) {
      if (id) {
        delete this.recipeCache[id];
        delete this.lastFetchTimes[`detail-${id}`];
      } else {
        // 清除所有食谱缓存
        this.recipeCache = {};
        this.recipeListCache = {};
        this.lastFetchTimes = {};
      }
    }
  }
}) 